package gat.api.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.bitsun.core.common.auth.shiro.jwt.JWTRealm;
import com.bitsun.core.common.config.WebMvcConfig;
import com.bitsun.core.common.mybatis.configuration.MybatisPlusConfiguration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
/**
 * 启动要加载的配置类
 * @author: tmac
 * @email: tmac
 */
@Import({WebMvcConfig.class, JWTRealm.class, MybatisPlusConfiguration.class})
@Configuration
@RefreshScope
public class GatConfig {

    @Configuration
    class SwaggerDocumentationConfig {

        ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("gat").description("gat")
                .license("").licenseUrl("http://unlicense.org")
                .termsOfServiceUrl("").version("1.0.0").contact(new Contact("", "", "")).build();
        }

        @Bean
        public Docket customImplementation() {
            return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("gat")).build()
                    .securitySchemes(securitySchemes())
                    .securityContexts(securityContexts())
                    .apiInfo(apiInfo())
                    .enable(enableSwagger)
                    ;
        }

        private List<ApiKey> securitySchemes() {
            List<ApiKey> apiKeys = new ArrayList<>();
            apiKeys.add(new ApiKey("x-tenant-id", "x-tenant-id", "header"));
            apiKeys.add(new ApiKey("x-app-id", "x-app-id", "header"));
            apiKeys.add(new ApiKey("x-app-token", "x-app-token", "header"));
            apiKeys.add(new ApiKey("x-user-token", "x-user-token", "header"));
            return apiKeys;
        }

        private List<SecurityContext> securityContexts() {
            List<SecurityContext> securityContexts = new ArrayList<>();
            securityContexts.add(SecurityContext.builder()
                    .securityReferences(defaultAuth())
                    .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
            return securityContexts;
        }

        private List<SecurityReference> defaultAuth() {
            AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
            AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
            authorizationScopes[0] = authorizationScope;
            List<SecurityReference> securityReferences = new ArrayList<>();
            securityReferences.add(new SecurityReference("x-tenant-id", authorizationScopes));
            securityReferences.add(new SecurityReference("x-app-id", authorizationScopes));
            securityReferences.add(new SecurityReference("x-app-token", authorizationScopes));
            securityReferences.add(new SecurityReference("x-user-token", authorizationScopes));
            return securityReferences;
        }

        @Value("${swagger.enable:true}")
        private boolean enableSwagger;

        //根据包进行分组
        @Bean
        public Docket createRestApiForBackground() {
            return new Docket(DocumentationType.SWAGGER_2).enable(true).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("gat.api.rest.background"))
                    .paths(PathSelectors.any()).build().groupName("后台").pathMapping("/")
                    .enable(enableSwagger)
                    ;
        }

        @Bean
        public Docket createRestApiForForeground() {
            return new Docket(DocumentationType.SWAGGER_2).enable(true).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("gat.api.rest.foreground"))
                    .paths(PathSelectors.any()).build().groupName("前台").pathMapping("/")
                    .enable(enableSwagger)
                    ;
        }

    }


}